//
//  ChangePasswordViewController.swift
//  TalkeTv
//
//  Created by lhf on 2024/11/7.
//

import UIKit
import Toast_Swift
import SwiftyJSON
import Alamofire

class ChangePasswordViewController: BaseViewController {
    
    @IBOutlet weak var originPassTextField: UITextField!
    @IBOutlet weak var newPassTextField: UITextField!
    @IBOutlet weak var confirmPassTextField: UITextField!
    
    @IBOutlet weak var submitBtn: TDisableButton!

    @IBAction func submit(_ sender: Any) {
        if newPassTextField.text != confirmPassTextField.text {
            self.view.makeToast("密码不一致", duration: 1.0, position: .center)
            return
        }
        
        var params = JSON.init(parseJSON: "{}")
        params["oldPassword"].string = originPassTextField.text
        params["newPassword"].string = newPassTextField.text
        params["confirmPassword"].string = confirmPassTextField.text
        
        APIManager.sharedInstance.sendRequest(urlString: APIManager.sharedInstance.change_password_action, method: HTTPMethod.post, params: params) { (result, error) in
            if error == nil {
                self.view.makeToast("修改成功", duration: 1.0, position: .center)
            }
        }
    }
    
    @objc func checkSubmitBtn() {
        if originPassTextField.text!.count > 0 && newPassTextField.text!.count > 0 && confirmPassTextField.text!.count > 0 {
            submitBtn.changeDisableStatus(true)
        } else {
            submitBtn.changeDisableStatus(false)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        originPassTextField.addTarget(self, action: #selector(checkSubmitBtn), for: .editingChanged)
        newPassTextField.addTarget(self, action: #selector(checkSubmitBtn), for: .editingChanged)
        confirmPassTextField.addTarget(self, action: #selector(checkSubmitBtn), for: .editingChanged)
        submitBtn.changeDisableStatus(false)

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
